const router = require('koa-router')()
const util = require('../utils/util')
const webData = require('../services/webData')

// 后台数据接口
router.post('/getAllData', async (ctx) => {
  try {
    const weekRes = await webData.getWeekView();
    const pageRes = await webData.getPageView();
    const userRes = await webData.getUserRes();
    const postNum = await webData.getPostNum();
    const data = {
      viewsOfTheWeek: weekRes[0],
      pageView: pageRes[0],
      userRegistrations: userRes[0],
      postNum: postNum[0]
    }
    ctx.body = util.success(data);
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

// 板块数据接口
router.post('/getAllSections', async (ctx) => {
  try {
    const res = await webData.getAllSections()
    console.log(res);
    ctx.body = util.success(res);
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

// 删除板块数据接口
router.post('/delectSection', async (ctx) => {
  try {
    const { sid } = ctx.request.body;
    await webData.delectSection(sid)
    ctx.body = util.success('', '', '删除成功');
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

// 所有用户数据接口
router.post('/getAllUser', async (ctx) => {
  try {
    const { uid, token } = ctx.request.body;
    // 验证 token
    const decoded = util.decoded(token);
    console.log(decoded);
    if (Number(decoded.res[0].uid)) {
      const res = await webData.getAllUser()
      console.log(res);
      ctx.body = util.success(res);
    } else {
      ctx.body = util.fail('业务请求失败');
    }
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})
// 增加板块数据接口
router.post('/addSection', async (ctx) => {
  try {
    const { sid, name, detail, isInset } = ctx.request.body;
    const res = await webData.addSection(sid, name, detail, Number(isInset))
    if (Number(isInset)) {
      ctx.body = util.success('', '', '添加成功');
    } else {
      ctx.body = util.success('', '', '修改成功');
    }
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

// 修改用户数据接口
router.post('/updataInfoByAdmin', async (ctx) => {
  try {
    const {
      email,
      password,
      phone,
      role,
      uid,
      username,
      token,
      adminId,
      adminPwd,
      adminUser
    } = ctx.request.body;
    // 验证 token
    const decoded = util.decoded(token);
    console.log(decoded);
    if (decoded.res[0].username === adminUser && decoded.res[0].password === adminPwd && adminId) {
      const res = await webData.updataInfoByAdmin(
        email,
        password,
        phone,
        role,
        uid,
        username)
      console.log(res);
      ctx.body = util.success(res);
    } else {
      ctx.body = util.fail('业务请求失败');
    }
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

// 删除用户数据接口
router.post('/deleteUser', async (ctx) => {
  try {
    const { uid } = ctx.request.body;
    await webData.deleteUser(uid)
    ctx.body = util.success('', '', '删除成功');
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

// 网站板块接口
router.post('/getAllPost', async (ctx) => {
  try {
    const res = await webData.getAllPost()
    console.log(res);
    ctx.body = util.success(res);
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

// 全部帖子数据接口
router.post('/getAllPost', async (ctx) => {
  try {
    const res = await webData.getAllPost()
    console.log(res);
    ctx.body = util.success(res);
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

// 删除帖子数据接口
router.post('/goToRecycle', async (ctx) => {
  try {
    const { sid, pid } = ctx.request.body;
    await webData.goToRecycle(sid, pid)
    ctx.body = util.success('', '', '删除成功');
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})
// 更改帖子数据接口
router.post('/updatePost', async (ctx) => {
  try {
    const { sid, name, intro, story, pid, portrait, isAdd } = ctx.request.body;
    if (Number(isAdd)) {
      await webData.addPost(name, intro, story, portrait, Number(sid));
      ctx.body = util.success('', '', '添加成功');
    } else {
      await webData.updatePost(name, intro, story, portrait, pid)
      ctx.body = util.success('', '', '更改成功');
    }
  } catch (error) {
    console.log(error);
    ctx.body = util.fail(error);
  }
})

module.exports = router